package com.sakura.mall.member.integrationChangeHistory.service.impl;

import com.sakura.mall.common.response.Page;
import com.sakura.mall.member.integrationChangeHistory.domain.entity.IntegrationChangeHistoryEntity;
import com.sakura.mall.member.integrationChangeHistory.domain.request.AddIntegrationChangeHistoryRequest;
import com.sakura.mall.member.integrationChangeHistory.domain.request.UpdateIntegrationChangeHistoryRequest;
import com.sakura.mall.member.integrationChangeHistory.domain.request.IntegrationChangeHistoryPageRequest;
import com.sakura.mall.member.integrationChangeHistory.domain.vo.IntegrationChangeHistoryVo;
import com.sakura.mall.member.integrationChangeHistory.mapper.IntegrationChangeHistoryMapper;
import com.sakura.mall.member.integrationChangeHistory.service.IntegrationChangeHistoryService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * 积分变化历史记录 service impl
 *
 * @author sakura
 */
@Service
@RequiredArgsConstructor
public class IntegrationChangeHistoryServiceImpl extends ServiceImpl<IntegrationChangeHistoryMapper, IntegrationChangeHistoryEntity> implements IntegrationChangeHistoryService {

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void add(AddIntegrationChangeHistoryRequest request) {
        // todo 其他校验逻辑
        IntegrationChangeHistoryEntity integrationChangeHistory = request.convert(IntegrationChangeHistoryEntity.class);
        this.save(integrationChangeHistory);
    }

    @Override
    public Page<IntegrationChangeHistoryVo> queryPage(IntegrationChangeHistoryPageRequest request) {
        IPage<IntegrationChangeHistoryEntity> qp = request.toPage();
        LambdaQueryWrapper<IntegrationChangeHistoryEntity> queryWrapper = new LambdaQueryWrapper<>();
        this.page(qp, queryWrapper);
        IPage<IntegrationChangeHistoryVo> ret = qp.convert(entity -> entity.convert(IntegrationChangeHistoryVo.class));
        return Page.convert(ret);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void updateById(Long id, UpdateIntegrationChangeHistoryRequest request) {
        IntegrationChangeHistoryEntity entity = request.convert(IntegrationChangeHistoryEntity.class);
        entity.setId(id);
        this.updateById(entity);
    }

}

